﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace 地声电源电路控制软件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] tx = new byte[10];
            if (checkBox1.Checked)
                tx[0] = 0xff;
            else
                tx[0] = 0;

            if (checkBox2.Checked)
                tx[1] = 0xff;
            else
                tx[1] = 0;

            if (checkBox3.Checked)
                tx[2] = 0xff;
            else
                tx[2] = 0;

            if (checkBox4.Checked)
                tx[3] = 0xff;
            else
                tx[3] = 0;

            if (checkBox5.Checked)
                tx[4] = 0xff;
            else
                tx[4] = 0;

            if (checkBox6.Checked)
                tx[5] = 0xff;
            else
                tx[5] = 0;

            if (checkBox7.Checked)
                tx[6] = 0xff;
            else
                tx[6] = 0;

            if (checkBox8.Checked)
                tx[7] = 0xff;
            else
                tx[7] = 0;

            CP1616Packet ret = new CP1616Packet(1);
            byte[] txList = CP1616Packet.MakeCP1616Packet(1, tx);
            serialPort.PortName = cbCom.Text;
            if (!CP1616Packet.OpenSerialPort(ref serialPort, ""))
                return;
            serialPort.Write(txList, 0, txList.Length);
            while (true)
            {
                try
                {
                    if (ret.DataPacketed((byte)serialPort.ReadByte()))
                    {
                        if (ret.RxList[4] == 'O' && ret.RxList[5] == 'K')
                        {
                            MessageBox.Show("设置成功");
                            serialPort.Close();
                            return;
                        }
                    }
                }
                catch// (Exception ex)
                {
                    //Debug.WriteLine("血糖通信失败" + ex.Message);
                    break;
                }
            }
            serialPort.Close();
            MessageBox.Show("设置失败");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CP1616Packet ret = new CP1616Packet(2);
            byte[] txList = CP1616Packet.MakeCP1616Packet(2, null);
            serialPort.PortName = cbCom.Text;
            if (!CP1616Packet.OpenSerialPort(ref serialPort, ""))
                return;
            serialPort.Write(txList, 0, txList.Length);
            while (true)
            {
                try
                {
                    if (ret.DataPacketed((byte)serialPort.ReadByte()))
                    {
                        if (ret.RxCount==10)
                        {                            
                            float t = ret.RxList[4] * 256 + ret.RxList[5];
                            t = t / 100;
                            textBox1.Text = t.ToString("F2");
                            serialPort.Close();
                            MessageBox.Show("读取成功");
                            return;
                        }
                    }
                }
                catch// (Exception ex)
                {
                    //Debug.WriteLine("血糖通信失败" + ex.Message);
                    break;
                }
            }
            serialPort.Close();
            MessageBox.Show("读取失败");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CP1616Packet ret = new CP1616Packet(5);
            byte[] txList = CP1616Packet.MakeCP1616Packet(5, null);
            serialPort.PortName = cbCom.Text;
            if (!CP1616Packet.OpenSerialPort(ref serialPort, ""))
                return;
            serialPort.Write(txList, 0, txList.Length);
            while (true)
            {
                try
                {
                    if (ret.DataPacketed((byte)serialPort.ReadByte()))
                    {

                        textBox2.Text = ret.RxList[4].ToString("X2") + " " + ret.RxList[5].ToString("X2");
                        serialPort.Close();
                        MessageBox.Show("读取成功");
                        return;
                    }
                }
                catch// (Exception ex)
                {
                    //Debug.WriteLine("血糖通信失败" + ex.Message);
                    break;
                }
            }
            serialPort.Close();
            MessageBox.Show("读取失败");
        }   
    }
}
